using AutoMapper;
using B.S.Data.MES.API.Applications.BaseData.Command.SupplierCommand;
using B.S.Domain.BasicData.SupplierManagement;
using B.S.Infratrctrue;
using B.S.Requst;
using MediatR;

namespace B.S.Data.MES.API.Applications.BaseData.CommandHandlers.SupplierCommandHandlers
{
    public class UpdateSupplierCommandHandlers : IRequestHandler<UpdateSupplierCommand, APIRequst<int>>
    {
        private readonly IBaseRepository<Supplier> supplierRepository;
        private readonly IMapper mapper;

        public UpdateSupplierCommandHandlers(IBaseRepository<Supplier> supplierRepository, IMapper mapper)
        {
            this.supplierRepository = supplierRepository;
            this.mapper = mapper;
        }

        public async Task<APIRequst<int>> Handle(UpdateSupplierCommand request, CancellationToken cancellationToken)
        {
            APIRequst<int> response = new APIRequst<int>();
            response.Code = RequstCode.成功;
            response.Msg = RequstCode.成功.ToString();
            var existingSupplier = mapper.Map<Supplier>(request);
            existingSupplier.UpdateTime = DateTime.Now;
            await supplierRepository.Update(existingSupplier);
            return response;
        }
    }
}